using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// PLC配置表Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(IPlcConfigService), ServiceLifetime = LifeTime.Transient)]
    public class PlcConfigService : BaseService<PlcConfig>, IPlcConfigService
    {
        /// <summary>
        /// 查询PLC配置表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<PlcConfigDto> GetList(PlcConfigQueryDto parm)
        {
            var predicate = QueryExp(parm);

            var response = Queryable()
                .Where(predicate.ToExpression())
                .ToPage<PlcConfig, PlcConfigDto>(parm);

            return response;
        }

        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public PlcConfig GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加PLC配置表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public PlcConfig AddPlcConfig(PlcConfig model)
        {
            return Insertable(model).ExecuteReturnEntity();
        }

        public PlcConfig QueryInfoByLineAndStation(string line, string op)
        {
            return Queryable().First(w => w.ProductionLineCode.Contains(line) && w.WorkstationCode.Contains(op));
        }

        /// <summary>
        /// 修改PLC配置表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdatePlcConfig(PlcConfig model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<PlcConfig> QueryExp(PlcConfigQueryDto parm)
        {
            var predicate = Expressionable.Create<PlcConfig>();

            return predicate;
        }
    }
}